'''
被测系统接口封装
增加员工：http://ihrm-test.itheima.net/api/sys/user
1.创建class
    1.1 实例化对象自动调用方法__init__(self)
        # __init__(self)的方法不需要调用，实例化对象时自动调用
    1.2 登录接口
'''
import unittest
import requests
import app


class EmployeeApi(unittest.TestCase):
    def __init__(self):
        # 设置强求头
        self.headers = app.headers
        self.add_url = app.BASE_URL + "/api/sys/user"
        self.search_url = app.BASE_URL + "/api/sys/user/{}"
        self.up_url = app.BASE_URL + "/api/sys/user/{}"
        self.del_url = app.BASE_URL + "/api/sys/user/{}"
        self.list_url = app.BASE_URL + "/api/sys/user"

    def add_employee(self):
        # 设置请求参数
        json_data = {"username": "皮卡丘23344",
                     "mobile": "13595977486",
                     "timeOfEntry": "2020-07-09",
                     "formOfEmployment": 1,
                     "workNumber": "10086",
                     "departmentName": "销售",
                     "departmentId": "1266699057968001024",
                     "correctionTime": "2020-07-30T16:00:00.000Z"
                     }
        response = requests.post(url=self.add_url, json=json_data, headers=self.headers)
        # 获取员工ID

        return response

    def search_employee(self, userId):
        search_url = self.search_url.format(userId)
        print(search_url)
        response = requests.get(url=search_url, headers=self.headers)
        return response

    def up_employee(self, userId, up_data):
        up_url = self.up_url.format(userId)
        response = requests.put(url=up_url, headers=self.headers, json=up_data)
        return response

    def del_employee(self, userId):
        del_url = self.del_url.format(userId)
        response = requests.delete(url=del_url, headers=self.headers)
        return response

    def list_employee(self):
        # params 可以使用dirt(json数据) /str 格式
        param = {"page": 5, "size": 11}
        response = requests.get(url=self.list_url, params=param, headers=self.headers)
        return response
